﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Wpf.Ui.Controls;

namespace ReNameEx.Model
{
    /// <summary>
    /// 排序规则
    /// </summary>
    public class SortRule : ReExRuleBase
    {
        /// <summary>
        /// 获取规则描述
        /// </summary>
        /// <returns></returns>
        public override string GetDesc()
        {
            StringBuilder sb = new StringBuilder();
            sb.Append($"在位置{InsertIndex}插入[{SortData.First()}-{SortData.Last()}]");
            sb.Append($"，起始{StartNum}");
            sb.Append($"，递增{Increment}");
            sb.Append($"，重复{Replication}次");
            if (ResetInterval == 0)
                sb.Append("，不重置");
            else
                sb.Append($"，每隔{ResetInterval}个重置");
            sb.Append($"，补零{ZeroFill}位");
            return sb.ToString();
        }

        public override void PrewRule(IEnumerable<ReExFile> files)
        {
            //应用规则
            if (Enable)
            {
                int curOrderIndex = StartNum;
                int replicationCount = Replication;
                int resetInterval = ResetInterval;
                foreach (ReExFile file in files)
                {
                    if (file.IsApply)
                    {
                        // 重置逻辑
                        if (resetInterval == 0)
                        {
                            curOrderIndex = StartNum;
                            resetInterval = ResetInterval;
                            replicationCount = Replication;
                        }
                        // 构建要插入的字符串
                        string toInsert = ConvertToCustom(curOrderIndex);
                        if (ZeroFill > 0)
                        {
                            toInsert = toInsert.PadLeft(ZeroFill, '0');
                        }
                        file.NewFileName = file.NewFileName.Insert(InsertIndex, toInsert);
                        // 处理重复次数
                        if (--replicationCount == 0)
                        {
                            replicationCount = Replication;
                            curOrderIndex += Increment;
                        }
                        resetInterval--;
                    }
                }
            }
        }

        /// <summary>
        /// 将顺序序号转为自定义序号
        /// </summary>
        /// <param name="n"></param>
        /// <returns></returns>
        /// <exception cref="ArgumentException"></exception>
        private string ConvertToCustom(int n)
        {
            if (n < 0) throw new ArgumentException("输入必须为非负整数");
            int sortlen = SortData.Length;

            if (n < sortlen)
            {
                return SortData[n];
            }

            Stack<string> resultStack = new Stack<string>();

            while (n > 0)
            {
                int remainder = n % sortlen;
                resultStack.Push(SortData[remainder]);
                n = n / sortlen;
            }

            // 拼接结果
            return string.Concat(resultStack);
        }

        /// <summary>
        /// 起始序号
        /// </summary>
        public int StartNum { get; set; } = 1;
        /// <summary>
        /// 序号增量
        /// </summary>
        public int Increment { get; set; } = 1;
        /// <summary>
        /// 重复次数
        /// </summary>
        public int Replication { get; set; } = 1;
        /// <summary>
        /// 插入位置
        /// </summary>
        public int InsertIndex { get; set; } = 0;
        /// <summary>
        /// 补零长度
        /// </summary>
        public int ZeroFill { get; set; } = 0;
        /// <summary>
        /// 重置间隔
        /// </summary>
        public int ResetInterval { get; set; } = 0;
        /// <summary>
        /// 排序数据
        /// </summary>
        public string[] SortData { get; set; } = ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9"];
    }
}
